﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ivory.GUI
{
    public abstract class GUIComponent : DrawableGameComponent
    {
        //Fields 
		private string _ID;
        public string ID
		{
            get { return _ID; }
            set { _ID = value; }
		}

        private bool _IsVisible = true;
        public bool IsVisible
        {
            get { return _IsVisible; }
            set { _IsVisible = value; }
        } 		

        private Rectangle _ClickRectangle = Rectangle.Empty;
        public Rectangle ClickRectangle
        {
            get { return _ClickRectangle; }
            protected set { _ClickRectangle = value; }
        }

        public event EventHandler LeftClicked;
        public event EventHandler RightClicked;
        public event EventHandler OnMouseEnter;
        public event EventHandler OnMouseLeave;

        private bool LastFrameHoverFlag = false;

        //cTor
        public GUIComponent(Game game, Rectangle ClickRectangle)
            : base(game)
        {
            this.ClickRectangle = ClickRectangle;
        }

        //Methods
        protected abstract void ResizeComponent();
        public abstract void DrawComponent(SpriteBatch batch, GameTime time);
        public abstract void LoadComponent();

        public virtual void UpdateComponent(InputControl input, GameTime time)
        {
            if (ClickRectangle != null)
            {
                if (ClickRectangle.Contains(input.MousePoint))
                {
                    if (!LastFrameHoverFlag)
                        if (OnMouseEnter != null)
                            OnMouseEnter(this, null);

                    LastFrameHoverFlag = true;
                    if (input.IsLeftClick())
                    {
                        if (LeftClicked != null)
                            LeftClicked(this, null);
                    }
                    else if (input.IsRightClick())
                    {
                        if (RightClicked != null)
                            RightClicked(this, null);
                    }
                }
                else
                {
                    if (LastFrameHoverFlag)
                    {
                        if (OnMouseLeave != null)
                        {
                            LastFrameHoverFlag = false;
                            OnMouseLeave(this, null);
                        }
                    }
                }
            }
        }        
    }
}
